#coding=utf-8
'''
Created on 2009-5-4

@author: Administrator
'''
from google.appengine.ext import webapp
from google.appengine.api import users


class BlogRequestHandler(webapp.RequestHandler):
    global siteroot
    siteroot = r'^[\w-\/]*$'
    def __init__(self):
        pass
    
    def initialize(self,request,response):
        webapp.RequestHandler.initialize(self,request,response)
        self.login_user = users.get_current_user()
        self.is_login = (self.login_user != None)
#        if self.is_login:
#            self.user = db.UserProperty(required = True)
#        else:
#            self.user = None
            
        self.is_admin = users.is_current_user_admin()
        if self.is_admin:
            self.auth = 'admin'
        else:
            self.auth = 'guest'
            
        try:
            self.referer = self.request.headers['referer']
        except:
            self.referer = None
            
        def get_login_url(self,from_referer=False):
            if from_referer:
                dst = self.referer
                if not dst : dst = siteroot
                return users.create_login_url(dst)
            else:
                return users.create_login_url(self.request.uri)
            
        def get_logout_url(self,from_referer=False):
            if from_referer:
                dst = self.referer
                if not dst : dst = siteroot
                return users.create_logout_url(dst)
            else:
                return users.create_logout_url(self.request.uri)
            
        def chk_login(self,redirect_url = siteroot):
            if self.is_login:
                return True
            else:
                return self.redirect(redirect_url)
                return False
            
        def chk_admin(self,redirect_url = siteroot):
            if self.is_admin:
                return True
            else:
                return self.redirect(redirect_url)
                return False
            
            
            
            
